package com.jamaskii.dictation;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.jamaskii.ecdict.Word;
import com.jamaskii.thread.Task;
import com.jamaskii.thread.TaskGroup;



public class MultiAddActivity extends BaseActivity {


    public int getStatueBarHeight(){
        int statusBarHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
            statusBarHeight = getResources().getDimensionPixelSize(height);
            return statusBarHeight;
        } catch (Exception e) {
            return 24;
        }
    }

    private void setTopPadding(){
        ConstraintLayout layout=findViewById(R.id.padding) ;
        ViewGroup.LayoutParams layoutParams=layout.getLayoutParams();
        layoutParams.height=getStatueBarHeight();
        layout.setLayoutParams(layoutParams);
    }

    private Handler hand=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_multi_add);
        init();
    }

    private void init(){
        ImageView imageBack=findViewById(R.id.image_back);
        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTopPadding();

    }

    public void onAddClick(View view){
        String text=((EditText)findViewById(R.id.textbox_words)).getText().toString();
        if(text.isEmpty()){
            return;
        }
        final String[] wordsList=text.split("\n");
        final ProgressDialog dialog=new ProgressDialog(this);
        dialog.setTitle("匹配单词");
        dialog.setMessage("进度:");
        dialog.show();
        final TaskGroup group=new TaskGroup();
        for (final String currentLine:wordsList
             ) {
            if(currentLine.equals("") || BookActivity.instance.book.isExist(currentLine)){
                continue;
            }
            Task task=new Task(new Runnable() {
                @Override
                public void run() {
                    Word word=Word.matchOnline(currentLine);
                    if(word==null){
                        HomeActivity.instance.showMessage("警告：单词匹配失败，判定为汉字："+currentLine);
                        int split = currentLine.indexOf(" ");
                        if(split==-1) {
                            BookActivity.instance.book.words.add(new Word(currentLine, "暂无释义"));
                        }else {
                            BookActivity.instance.book.words.add(
                                    new Word(currentLine.substring(0,split),currentLine.substring(split)));
                        }
                        return;
                    }
                    BookActivity.instance.book.words.add(word);
                }
            });
            task.onDone=new Runnable() {
                @Override
                public void run() {
                    dialog.setMessage("进度："+group.getDoneTask().size()+"/"+group.tasks.size());
                }
            };
            group.tasks.add(task);
        }
        group.start(8, new Runnable() {
            @Override
            public void run() {
                BookActivity.instance.book.save(HomeActivity.instance.wordBookStream.path);
                BookActivity.hand.post(new Runnable() {
                    @Override
                    public void run() {
                        BookActivity.instance.showWords();
                    }
                });
                hand.post(new Runnable() {
                    @Override
                    public void run() {
                        dialog.cancel();
                        finish();
                    }
                });
            }
        });
    }

}